/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ants;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 *
 * @author Sagrevyum
 */
public class cliente {

    static int foodRest;
    static GamePlay world;
    static LinkedList moves;

    public static void main(String[] args) throws ClassNotFoundException, IOException {

        cliente client = new cliente();

    }

    cliente() throws ClassNotFoundException, IOException {
        try { 
            Socket socket = new Socket("localhost", 5000); 
            InputStream aux = socket.getInputStream();
            ObjectInputStream entradaObjetos = new ObjectInputStream(aux);
            OutputStream aux2 = socket.getOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(aux2);

            // Leer estado inicial
            Inicial mapa;
            mapa = (Inicial) entradaObjetos.readObject();
            System.out.println("Recibi el mapa");

            ArrayList lista;
            lista = new ArrayList();
            int k = 1;
            for (int x = 0; x < mapa.getExploradoras(); x++) {
                lista.add(new Hormiga(k++, 0));
            }
            for (int x = 0; x < mapa.getGuerreras(); x++) {
                lista.add(new Hormiga(k++, 1));
            }
            for (int x = 0; x < mapa.getObreras(); x++) {
                lista.add(new Hormiga(k++, 2));
            }

            while (true) {
                // Recibir vistas
                AntsView vista;                
                vista = (AntsView) entradaObjetos.readObject();
                System.out.println("Recibi el AntsView");
                // Logica
                MoveList movimientos = new MoveList();
                ListIterator itr = lista.listIterator();
                
                while (itr.hasNext()) {
                    Hormiga ant = (Hormiga) itr.next();
                    Perspective p = (Perspective)vista.perspectiva.get(ant.id);
                    movimientos.agregar(new Move(ant.id, ant.getMovimiento((Perspective)vista.perspectiva.get(ant.id))));
                }
                // Mandar movimientos
                os.writeObject(movimientos);
                System.out.println("Mande los movimientos");
                try{
                    Thread.sleep (300);
                }catch(Exception e){
                        
                }
            }
        }
        catch(Exception e){
        
        }
    }
}